package control;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.AvailableDTO;
import model.FacilityDTO;
import business.BookingManager;

/**
 * Servlet implementation class AvailableServlet
 */
@WebServlet(name = "Available", urlPatterns = { "/Available" })
public class AvailableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AvailableServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String startdate = request.getParameter("timestamp");
		String Type = request.getParameter("facilityType");
		String period = request.getParameter("period");
		String priority = request.getParameter("priority");
	    System.out.println("Timestamp: " + startdate);
		System.out.println("Type: " + Type);
		System.out.println("Period: " + period);
		Map<String, String> errors = new HashMap<String, String>();

		String origin = request.getParameter("period");
		String origin2 = request.getParameter("timestamp");
		if (origin.isEmpty()) {
			errors.put("origin", "Enter Period.");
		} else {
			errors.remove("origin");
		}
		if (origin2.isEmpty()) {
			errors.put("origin2", "Enter Start Date");
		} else {
			errors.remove("origin2");
		}
		if (errors.isEmpty()) {
			AvailableDTO booking = new AvailableDTO();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			Date dStart = null;
			try {
				dStart = df.parse(startdate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			booking.setPeriod(Integer.valueOf(period));
			booking.setStartDate(dStart);
			booking.setFacilityType(Type);
			booking.setPriority(priority);
			BookingManager bm = new BookingManager();
		 ArrayList<FacilityDTO> facilitylist = bm.findfacilityByCriteria(booking);
		  request.setAttribute("searchlist", facilitylist);
		  RequestDispatcher rd;
		    rd = request.getRequestDispatcher("/Available.jsp");
			rd.forward(request, response);
		} else {
			request.setAttribute("errors", errors);
			request.getRequestDispatcher("Available.jsp").forward(request,
					response);
		}
	}
}
